package org.colcon.web.client.ui.deprecated;

import java.util.HashMap;
import java.util.Map;

import org.colcon.web.client.Collective_Congress;
import org.colcon.web.model.framework.IPower;
import org.colcon.web.model.framework.ITitle;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PowerInfoPanel extends InfoPanel
{
	private VerticalPanel panel;
	private Grid grid;
	private TextBox[] boxes;
	
	private ITitle title;
	
	public PowerInfoPanel(Collective_Congress client, ITitle title, IPower power) 
	{
		super(client, power);
		
		this.title = title;
		
		panel = new VerticalPanel();
		
		if(!title.hasHolder(client.getUser().getId()))
		{
			panel.add(new Label("You do not hold this title"));
			add(panel);
			return;
		}
		
		String[] args = power.getDecisionArgs();
		boxes = new TextBox[args.length];
		
		grid = new Grid(args.length + 1, 2);
		
		for(int i = 0; i < args.length; i++)
		{
			String p = args[i];
			grid.setWidget(i, 0, new Label(p));
			
			TextBox box = new TextBox();
			boxes[i] = box;
			grid.setWidget(i, 1, box);
		}
		
		Button button = new Button("Submit");
		button.addClickHandler(new MyButtonHandler());
		grid.setWidget(args.length, 1, button);
		
		sinkEvents(Event.ONKEYPRESS);
		addHandler(new MyEnterHandler(), KeyPressEvent.getType());
		
		panel.add(grid);
		add(panel);
	}
	
	private IPower getPower()
	{
		return (IPower) getItem();
	}
	
	private Map<String, String> getParams()
	{
		String[] args = getPower().getDecisionArgs();
		Map<String, String> map = new HashMap<String, String>();
		
		for(int i = 0; i < args.length; i++)
			map.put(args[i], boxes[i].getText());
		
		return map;
	}
	
	private class MyButtonHandler implements ClickHandler
	{
		public void onClick(ClickEvent event) 
		{
			buttonClick();
		}
	}
	
	private class MyEnterHandler implements KeyPressHandler
	{
		public void onKeyPress(KeyPressEvent event) 
		{
			if(event.getCharCode() == KeyCodes.KEY_ENTER)
				buttonClick();
		}
	}
	
	public void buttonClick()
	{
		IPower power = (IPower)getPower();
		
		panel.clear();
		panel.add(new Label("Processing..."));
		
		getService().usePower(title.getLongName(), power.getId(), getParams(), new AsyncCallback<Boolean>()
		{
			public void onFailure(Throwable caught)
			{
				caught.printStackTrace();
				
				panel.clear();
				panel.add(new Label("Decision failed"));
			}
			
			public void onSuccess(Boolean result)
			{
				if(result)
				{
					panel.clear();
					panel.add(new Label("Decision complete"));
				}
				else
				{
					panel.clear();
					panel.add(new Label("Decision failed"));
				}
			}
		});
	}
}
